package androids.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import androids.modelo.UsuarioModelo;

@WebServlet("/UsuarioServlet")
public class UsuarioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

   
    public UsuarioServlet() {
        // TODO Auto-generated constructor stub
    }

    
    protected void processRequest(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    		String contentType= "text/html;charset=utf-8";
    		response.setContentType(contentType);
    		String opcion = request.getParameter("o");
    		UsuarioDAO dao = new UsuarioDAO();
    		if(opcion.equals("Nuevo")){
    			public int idUsuario;
    			public String nombre;
    			public String apPaterno;
    			public String apMaterno;
    			public String dni;
    			public String edad;
    			public String email;
    			public String direccion;
    			public Integer idHistorial;
    			public int getIdUsuario() {
    				return idUsuario;
    			UsuarioModelo vo = new UsuarioModelo();
    			vo.setNombre(request.getParameter("nombre"));
    			vo.setApPaterno(request.getParameter("apPaterno"));
    			vo.setApMaterno(request.getParameter("apMaterno"));
    			vo.setDni(request.getParameter("dni"));
    			vo.setEmail(request.getParameter("email"));
    			vo.setDireccion(request.getParameter("direccion"));
    			vo.setIdHistorial(request.getParameter("idHistorial"));
    			int idUsuario = dao.insertar(vo);
    			System.out.println("idUsuario -> " + idUsuario);
				request.setAttribute("resultado", idUsuario);
				RequestDispatcher rd = request.getRequestDispatcher("nuevo.jsp");
                rd.forward(request, response);
    		}else if(opcion.equals("Listar")){
    			Collection<UsuarioModelo> c = new ArrayList<UsuarioModelo>();
    			String ref = "";
    			if(request.getParameter("ref")!=null){
    				if(!request.getParameter("ref").equals("")){
    						ref = request.getParameter("ref");
    				}
    			}
    			c = dao.listarUsuarios(ref);
    			request.setAttribute("usuarios", c);
    			request.setAttribute("ref", ref);
				RequestDispatcher rd = request.getRequestDispatcher("listar.jsp");
                rd.forward(request, response);
    		}
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 processRequest(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 processRequest(request, response);
	}

}
